<template>
  <div id="player">
    <el-card style="background-color: #CDB79E;">
      <el-row>
        <el-col :span="1">
          <img id="audioPic" :src="picUrl">
        </el-col>
        <el-col :span="4">
          <div>
            <marquee>
              <h4>
                <i class="el-icon-mic"></i>
                {{this.artist}}：{{this.SongName}}
              </h4>
            </marquee>
          </div>
        </el-col>
        <el-col :span="8">
          <el-progress id="progress" :show-text="false" :stroke-width="10" color="#996600" define-back-color="#330000"
            :percentage="percent">
          </el-progress>
        </el-col>
        <el-col :span="1">
          <div id="time">
            {{this.time}}/{{this.allTime}}
          </div>
        </el-col>
        <el-col :span="3">
          <div id="playIcon">
            <i class="el-icon-d-arrow-left" @click="last()"></i>
            <i class="el-icon-video-pause" @click="musicPlay()" v-show="play!==false"></i>
            <i class="el-icon-video-play" @click="musicPause()" v-show="play!==true"></i>
            <i class="el-icon-d-arrow-right" @click="next()"></i>
          </div>
        </el-col>
        <el-col :span="5">
          <div class="bg">
            <h4 style="color:#38B0DE">{{this.text}}</h4>
          </div>
        </el-col>
        <el-col :span="1">
          <div style="text-align: center">
            <el-popover placement="top-start" width="500" trigger="click">
              <el-table :data="songList" highlight-current-row @row-dblclick="getSongId" height="500">
                <el-table-column type="index" width="50">
                </el-table-column>
                <el-table-column property="title" width="300" label="音乐"></el-table-column>
                <el-table-column property="name" label="歌手"></el-table-column>
              </el-table>
              <el-button id="playlist" type="text" slot="reference">播放列表</el-button>
            </el-popover>
          </div>
        </el-col>

        <audio v-show="false" id="audio" autoplay="autoplay" :src="url" @timeupdate="update()" @ended="ended()"
          controls></audio>
      </el-row>
    </el-card>
  </div>
</template>

<script>
  import musicApi from '@/musicApi/apis/songs'
  import music from "@/until/js/music";
  import songs from "@/musicApi/apis/songs";
  export default {
    name: "player",
    data() {
      return {
        picUrl: '',
        url: '',
        id: null,
        SongName: '暂无音乐',
        artist: '暂无歌手',
        songList: [],
        index: 0,
        percent: 100,
        allTime: '0:00',
        time: '0:00',
        play: true,
        lyricData: [],
        text: '',
        lyricIndex: 0
      }
    },
    mounted() {
      this.PicUrl = window.localStorage.getItem('picUrl');
      this.getSongUrl(window.localStorage.getItem('id'));
      this.getLyric(window.localStorage.getItem('id'))
      this.songList = JSON.parse(window.localStorage.getItem("songList"))
    },
    methods: {
      getSongUrl(id) {
        this.picUrl = this.$store.state.picUrl;
        this.artist = this.$store.state.name
        this.SongName = this.$store.state.title
        musicApi.SearchMusicUrl(id).then(res => {
          if (res.data.data[0].url == null) {
            this.$message.error("抱歉哦，没有版权")
          } else {
            this.url = res.data.data[0].url;
            this.lyricIndex = 0
          }

        })
      },
      //获取所在行的歌曲信息
      async getSongId(row) {
        this.$store.commit('SET_Song_Id', row);
      },
      //当audio进度条发生改变
      update() {
        var audio = document.querySelector("audio");
        let duration = audio.duration//总时长
        let currentTime = audio.currentTime//当前播放时间
        this.percent = (currentTime / duration) * 100
        this.allTime = music.dataFormate(duration * 1000)
        this.time = music.dataFormate(currentTime * 1000)
        if (this.time == this.lyricData[this.lyricIndex].substring(1, 6)) {
          this.text = this.lyricData[this.lyricIndex].split(']')[1]
          // this.text=this.lyricData[this.lyricIndex].substring(11,this.lyricData[this.lyricIndex].length)
          this.lyricIndex++;
          console.log(this.text)
        }
      },
      //当当前音乐播放完毕时（下一首）
      ended() {
        //当前标签index为末尾时
        if (this.index == this.songList.length - 1) {
          this.index = 0
        } else {
          this.index++
        }
        if (window.localStorage.getItem('id') != this.songList[this.index].id) {
          this.$store.commit('SET_Song_Id', this.songList[this.index]);
        }
      },
      //上一首
      lasted() {
        //当前标签为开头时
        if (this.index == 0) {
          this.index = this.songList.length - 1
        } else {
          this.index -= 1
        }
        if (window.localStorage.getItem('id') != this.songList[this.index].id) {
          this.$store.commit('SET_Song_Id', this.songList[this.index]);
        }

      },
      //音乐播放状态改为暂停
      musicPlay() {
        this.play = false
        var audio = document.querySelector("audio")
        audio.pause()
      },
      //音乐暂停状态改为播放
      musicPause() {
        this.play = true
        var audio = document.querySelector("audio")
        audio.play()
      },
      //上一首
      last() {
        this.lasted()
      },
      //下一首
      next() {
        this.ended()
      },
      //获取歌词
      getLyric(id) {
        songs.getLyric(id).then(res => {
          this.lyricData = res.data.lrc.lyric.split("\n")
          console.log(this.lyricData)
        })
      }
    },
    computed: {
      //播放歌曲id监听
      watchSongId() {
        return this.$store.state.id;
      },
      /*    //监控音乐是否播放完
          watchMusicOver(){
            return document.getElementById('audio').currentTime
          }*/
    },
    watch: {
      watchSongId: {
        handler(newId) {
          console.log('当前播放歌曲：' + window.localStorage.getItem('title') + "，index=" + this.index)
          this.getSongUrl(newId);
          this.getLyric(newId)
        }
      },

    }
  }
</script>

<style scoped>
  #player {
    width: 100%;
    font-size: 1rem;
    position: fixed;
    bottom: 0;
    left: 0;
  }

  #audioPic {
    width: 70px;
    height: 70px;
    float: left;
  }

  #audio {
    display: block;
    float: left;
    width: 80%;
  }

  #progress {
    width: 90%;
    top: 28px;
    left: 30px;
    float: left;
  }

  #playIcon {
    margin-top: 19px;
    cursor: pointer;
    text-align: center;
  }

  #playIcon i {
    font-size: 30px;
    margin-right: 20px;
  }

  #time {
    margin-top: 22px;
  }

  #playlist {
    margin-top: 15px;
    color: #990000;
  }

  .bg {
    border: 2px solid #CDB79E;
    background: #2F4F4F;
    text-align: center;
    width: 350px;
    /* 设置容器宽度 */
    height: 70px;
    overflow: hidden;
    white-space: nowrap;
  }

  .bg h4 {
    animation: marquee 5s linear infinite;
    /* 设置动画效果 */
  }

  @keyframes bg {
    0% {
      transform: translateX(0);
    }

    100% {
      transform: translateX(-100%);
    }
  }

  /* 隐藏伪元素 */
  ::v-deep.el-table::before {
    display: none !important;
  }

  /* // 更改表头背景颜色 */
  ::v-deep.el-table thead tr>th {
    background-color: #990000;
  }

  /* // 更改表格每行背景颜色 */
  ::v-deep.el-table tbody tr>td {
    background-color: #FFEAF0;
  }

  /* // 设置鼠标经过时背景颜色 */
  ::v-deep.el-table tbody tr:hover>td {
    background-color: #FFFFF0 !important;
  }
</style>